﻿using HardcoreGame.Common.Players;
using HardcoreGame.Content.Items.ItemIcons;
using System;
using System.Collections.Generic;
using Terraria;
using Terraria.DataStructures;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
namespace HardcoreGame.Common.GlobalItems
{
    public class FoodFreshnessInfo
    {
        public float BaseFreshness = 100f;
        public float SingleDecayRate = 0.1f;
        public float StackedDecayRate = 0.05f;
        public float SpoilageThreshold = 30f;
        public Action<Item, Player, float> OnConsumeEffect;
        public Action<Item, Player> OnSpoiledEffect;
        public Func<float, string> CustomTooltipFormat;
        public TagCompound Save()
        {
            return new TagCompound
            {
                ["base"] = BaseFreshness,
                ["single"] = SingleDecayRate,
                ["stacked"] = StackedDecayRate,
                ["spoilage"] = SpoilageThreshold
            };
        }
        public static FoodFreshnessInfo Load(TagCompound tag)
        {
            return new FoodFreshnessInfo
            {
                BaseFreshness = tag.GetFloat("base"),
                SingleDecayRate = tag.GetFloat("single"),
                StackedDecayRate = tag.GetFloat("stacked"),
                SpoilageThreshold = tag.GetFloat("spoilage")
            };
        }
    }
    public class IndependentFoodFreshnessSystem : GlobalItem
    {
        public static Dictionary<int, FoodFreshnessInfo> FoodDefinitions = [];
        public float CurrentFreshness { get; private set; } = 100f;
        public FoodFreshnessInfo FoodInfo { get; private set; }
        private bool initialized = false;
        public override bool InstancePerEntity => true;
        protected override bool CloneNewInstances => true;
        public override void SaveData(Item item, TagCompound tag)
        {
            if (FoodDefinitions.ContainsKey(item.type))
            {
                tag["freshness"] = CurrentFreshness;
                tag["initialized"] = initialized;
                if (FoodInfo != null)
                {
                    tag["foodInfo"] = FoodInfo.Save();
                }
            }
        }
        public override void LoadData(Item item, TagCompound tag)
        {
            if (FoodDefinitions.TryGetValue(item.type, out var info))
            {
                CurrentFreshness = tag.GetFloat("freshness");
                initialized = tag.GetBool("initialized");
                if (tag.ContainsKey("foodInfo"))
                {
                    FoodInfo = FoodFreshnessInfo.Load(tag.GetCompound("foodInfo"));
                }
                else
                {
                    FoodInfo = info;
                }
            }
        }
        public static void RegisterFood(int itemId, FoodFreshnessInfo info)
        {
            FoodDefinitions[itemId] = info;
        }
        public override void SetDefaults(Item item)
        {
            if (!initialized && FoodDefinitions.TryGetValue(item.type, out var info))
            {
                InitializeFreshness(item, info);
            }
            if (item.type == ItemID.CookedMarshmallow)
            {
                item.DefaultToFood(12, 12, 0, 0);
            }
            if (item.type == ItemID.AppleJuice)
            {
                item.DefaultToFood(22, 22, 0, 0, useGulpSound: true);
            }
            if (item.type == ItemID.BloodyMoscato)
            {
                item.DefaultToFood(22, 22, 0, 0, useGulpSound: true);
            }
            if (item.type == ItemID.BowlofSoup)
            {
                item.DefaultToFood(22, 22, 0, 0, useGulpSound: true);
            }
            if (item.type == ItemID.BunnyStew)
            {
                item.DefaultToFood(22, 22, 0, 0, useGulpSound: true);
            }
            if (item.type == ItemID.CookedFish)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.CookedShrimp)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Escargot)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.FroggleBunwich)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.FruitJuice)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.FruitSalad)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.GrapeJuice)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.GrilledSquirrel)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.GrubSoup)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Lemonade)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.LobsterTail)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.MonsterLasagna)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.PeachSangria)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.PinaColada)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.PrismaticPunch)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.RoastedBird)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.RoastedDuck)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.SauteedFrogLegs)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.SeafoodDinner)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.SmoothieofDarkness)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.TropicalSmoothie)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.PumpkinPie)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Teacup)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Sashimi)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.JojaCola)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Pho)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.PadThai)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Marshmallow)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.SugarCookie)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.GingerbreadCookie)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.ChristmasPudding)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Steak)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Spaghetti)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.ShuckedOyster)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.ShrimpPoBoy)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.PotatoChips)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Pizza)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Nachos)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Milkshake)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.IceCream)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Hotdog)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Grapes)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Fries)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.FriedEgg)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.CreamSoda)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.CoffeeCup)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.ChocolateChipCookie)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.ChickenNugget)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.MilkCarton)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Burger)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.BBQRibs)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.BananaSplit)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Bacon)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.ApplePie)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == 5277)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Starfruit)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Rambutan)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == 5278)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Plum)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Pineapple)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Peach)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Mango)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Lemon)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Grapefruit)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Elderberry)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Dragonfruit)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Coconut)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Cherry)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.BloodOrange)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.BlackCurrant)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Banana)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Apricot)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.Apple)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
            if (item.type == ItemID.GoldenDelight)
            {
                item.DefaultToFood(22, 22, 0, 0);
            }
        }
        public override bool CanUseItem(Item item, Player player)
        {
            if (item.type == ItemID.CookedMarshmallow)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
            }
            if (item.type == ItemID.AppleJuice)
            {
                item.stack--;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            }
            if (item.type == ItemID.BloodyMoscato)
            {
                item.stack--;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            }
            if (item.type == ItemID.BowlofSoup)
            {
                item.stack--;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 2;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 2;
            }
            if (item.type == ItemID.BunnyStew)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 3;
            }
            if (item.type == ItemID.CookedFish)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 2;
            }
            if (item.type == ItemID.CookedShrimp)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 2;
            }
            if (item.type == ItemID.Escargot)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
            }
            if (item.type == ItemID.FroggleBunwich)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 3;
            }
            if (item.type == ItemID.BananaDaiquiri)
            {
                item.stack--;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 2;
            }
            if (item.type == ItemID.FruitJuice)
            {
                item.stack--;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 2;
            }
            if (item.type == ItemID.FruitSalad)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 2;
            }
            if (item.type == ItemID.GrapeJuice)
            {
                item.stack--;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 2;
            }
            if (item.type == ItemID.GrilledSquirrel)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 2;
            }
            if (item.type == ItemID.GrubSoup)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 2;
            }
            if (item.type == ItemID.Lemonade)
            {
                item.stack--;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 2;
            }
            if (item.type == ItemID.LobsterTail)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 3;
            }
            if (item.type == ItemID.MonsterLasagna)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 3;
            }
            if (item.type == ItemID.PeachSangria)
            {
                item.stack--;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 2;
            }
            if (item.type == ItemID.PinaColada)
            {
                item.stack--;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 2;
            }
            if (item.type == ItemID.PrismaticPunch)
            {
                item.stack--;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 2;
            }
            if (item.type == ItemID.RoastedBird)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 2;
            }
            if (item.type == ItemID.RoastedDuck)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 3;
            }
            if (item.type == ItemID.SauteedFrogLegs)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 2;
            }
            if (item.type == ItemID.SeafoodDinner)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 4;
            }
            if (item.type == ItemID.SmoothieofDarkness)
            {
                item.stack--;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 2;
            }
            if (item.type == ItemID.TropicalSmoothie)
            {
                item.stack--;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 2;
            }
            if (item.type == ItemID.PumpkinPie)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 2;
            }
            if (item.type == ItemID.Teacup)
            {
                item.stack--;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 2;
            }
            if (item.type == ItemID.Sashimi)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 2;
            }
            if (item.type == 5275)
            {
                item.stack--;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 2;
            }
            if (item.type == ItemID.Pho)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 3;
            }
            if (item.type == ItemID.PadThai)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 3;
            }
            if (item.type == ItemID.Marshmallow)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
            }
            if (item.type == ItemID.SugarCookie)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
            }
            if (item.type == ItemID.GingerbreadCookie)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
            }
            if (item.type == ItemID.ChristmasPudding)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 2;
            }
            if (item.type == ItemID.Steak)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 3;
            }
            if (item.type == ItemID.Spaghetti)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 2;
            }
            if (item.type == ItemID.ShuckedOyster)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 2;
            }
            if (item.type == ItemID.ShrimpPoBoy)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 3;
            }
            if (item.type == ItemID.PotatoChips)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
            }
            if (item.type == ItemID.Pizza)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 3;
            }
            if (item.type == ItemID.Nachos)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 3;
            }
            if (item.type == ItemID.Milkshake)
            {
                item.stack--;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 2;
            }
            if (item.type == ItemID.IceCream)
            {
                item.stack--;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 2;
            }
            if (item.type == ItemID.Hotdog)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 2;
            }
            if (item.type == ItemID.Grapes)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            }
            if (item.type == ItemID.Fries)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 2;
            }
            if (item.type == ItemID.FriedEgg)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 2;
            }
            if (item.type == ItemID.CreamSoda)
            {
                item.stack--;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 2;
            }
            if (item.type == ItemID.CoffeeCup)
            {
                item.stack--;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 2;
            }
            if (item.type == ItemID.ChocolateChipCookie)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
            }
            if (item.type == ItemID.ChickenNugget)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 2;
            }
            if (item.type == ItemID.MilkCarton)
            {
                item.stack--;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 2;
            }
            if (item.type == ItemID.Burger)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 3;
            }
            if (item.type == ItemID.BBQRibs)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 3;
            }
            if (item.type == ItemID.BananaSplit)
            {
                item.stack--;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 2;
            }
            if (item.type == ItemID.Bacon)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 2;
            }
            if (item.type == ItemID.ApplePie)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 2;
            }
            if (item.type == 5277)
            {
                item.stack--;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            }
            if (item.type == ItemID.Starfruit)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            }
            if (item.type == ItemID.Rambutan)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            }
            if (item.type == 5278)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            }
            if (item.type == ItemID.Plum)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            }
            if (item.type == ItemID.Pineapple)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            }
            if (item.type == ItemID.Peach)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            }
            if (item.type == ItemID.Mango)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            }
            if (item.type == ItemID.Lemon)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            }
            if (item.type == ItemID.Grapefruit)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            }
            if (item.type == ItemID.Elderberry)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            }
            if (item.type == ItemID.Dragonfruit)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            }
            if (item.type == ItemID.Coconut)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            }
            if (item.type == ItemID.Cherry)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            }
            if (item.type == ItemID.BloodOrange)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            }
            if (item.type == ItemID.BlackCurrant)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            }
            if (item.type == ItemID.Banana)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            }
            if (item.type == ItemID.Apricot)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            }
            if (item.type == ItemID.Apple)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 1;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 1;
            }
            if (item.type == ItemID.GoldenDelight)
            {
                item.stack--;
                player.GetModPlayer<HungerPlayer>().StageForHunger -= 5;
                player.GetModPlayer<ThirstPlayer>().StageForThirst -= 5;
            }
            return base.CanUseItem(item, player);
        }
        private void InitializeFreshness(Item item, FoodFreshnessInfo info)
        {
            ArgumentNullException.ThrowIfNull(item);
            FoodInfo = info;
            CurrentFreshness = info.BaseFreshness;
            initialized = true;
        }
        public override void UpdateInventory(Item item, Player player)
        {
            if (FoodInfo == null) return;
            float decayRate = item.stack > 1 ? FoodInfo.StackedDecayRate : FoodInfo.SingleDecayRate;
            CurrentFreshness -= decayRate * (1f / 60f);
            CurrentFreshness = Math.Max(0, CurrentFreshness);
        }
        public override void SplitStack(Item destination, Item source, int numToTransfer)
        {
            if (FoodInfo == null) return;
            var sourceSystem = source.GetGlobalItem<IndependentFoodFreshnessSystem>();
            var destSystem = destination.GetGlobalItem<IndependentFoodFreshnessSystem>();
            destSystem.FoodInfo = sourceSystem.FoodInfo;
            destSystem.CurrentFreshness = sourceSystem.CurrentFreshness;
            destSystem.initialized = true;
        }
        public override void OnCreated(Item item, ItemCreationContext context)
        {
            if (!initialized && FoodDefinitions.TryGetValue(item.type, out var info))
            {
                InitializeFreshness(item, info);
            }
        }
        public override void ModifyTooltips(Item item, List<TooltipLine> tooltips)
        {
            if (FoodInfo != null)
            {
                tooltips.Add(new TooltipLine(Mod, "Freshness", GetFreshnessStars(CurrentFreshness)));
            }
            if (item.type == ItemID.CookedMarshmallow)
            {
                tooltips.Add(new TooltipLine(Mod, "1", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
            }
            if (item.type == ItemID.AppleJuice)
            {
                tooltips.Add(new TooltipLine(Mod, "2", Language.GetTextValue("Mods.HardcoreGame.ToolTip.2")));
            }
            if (item.type == ItemID.BloodyMoscato)
            {
                tooltips.Add(new TooltipLine(Mod, "3", Language.GetTextValue("Mods.HardcoreGame.ToolTip.2")));
            }
            if (item.type == ItemID.BowlofSoup)
            {
                tooltips.Add(new TooltipLine(Mod, "4", Language.GetTextValue("Mods.HardcoreGame.ToolTip.3")));
                tooltips.Add(new TooltipLine(Mod, "5", Language.GetTextValue("Mods.HardcoreGame.ToolTip.4")));
            }
            if (item.type == ItemID.BunnyStew)
            {
                tooltips.Add(new TooltipLine(Mod, "6", Language.GetTextValue("Mods.HardcoreGame.ToolTip.5")));
            }
            if (item.type == ItemID.CookedFish)
            {
                tooltips.Add(new TooltipLine(Mod, "7", Language.GetTextValue("Mods.HardcoreGame.ToolTip.3")));
            }
            if (item.type == ItemID.CookedShrimp)
            {
                tooltips.Add(new TooltipLine(Mod, "8", Language.GetTextValue("Mods.HardcoreGame.ToolTip.3")));
            }
            if (item.type == ItemID.Escargot)
            {
                tooltips.Add(new TooltipLine(Mod, "9", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
            }
            if (item.type == ItemID.FroggleBunwich)
            {
                tooltips.Add(new TooltipLine(Mod, "10", Language.GetTextValue("Mods.HardcoreGame.ToolTip.5")));
            }
            if (item.type == ItemID.BananaDaiquiri)
            {
                tooltips.Add(new TooltipLine(Mod, "11", Language.GetTextValue("Mods.HardcoreGame.ToolTip.4")));
            }
            if (item.type == ItemID.FruitJuice)
            {
                tooltips.Add(new TooltipLine(Mod, "12", Language.GetTextValue("Mods.HardcoreGame.ToolTip.4")));
            }
            if (item.type == ItemID.FruitSalad)
            {
                tooltips.Add(new TooltipLine(Mod, "13", Language.GetTextValue("Mods.HardcoreGame.ToolTip.3")));
            }
            if (item.type == ItemID.GrapeJuice)
            {
                tooltips.Add(new TooltipLine(Mod, "14", Language.GetTextValue("Mods.HardcoreGame.ToolTip.4")));
            }
            if (item.type == ItemID.GrilledSquirrel)
            {
                tooltips.Add(new TooltipLine(Mod, "15", Language.GetTextValue("Mods.HardcoreGame.ToolTip.3")));
            }
            if (item.type == ItemID.GrubSoup)
            {
                tooltips.Add(new TooltipLine(Mod, "16", Language.GetTextValue("Mods.HardcoreGame.ToolTip.3")));
            }
            if (item.type == ItemID.Lemonade)
            {
                tooltips.Add(new TooltipLine(Mod, "17", Language.GetTextValue("Mods.HardcoreGame.ToolTip.4")));
            }
            if (item.type == ItemID.LobsterTail)
            {
                tooltips.Add(new TooltipLine(Mod, "18", Language.GetTextValue("Mods.HardcoreGame.ToolTip.5")));
            }
            if (item.type == ItemID.MonsterLasagna)
            {
                tooltips.Add(new TooltipLine(Mod, "19", Language.GetTextValue("Mods.HardcoreGame.ToolTip.5")));
            }
            if (item.type == ItemID.PeachSangria)
            {
                tooltips.Add(new TooltipLine(Mod, "20", Language.GetTextValue("Mods.HardcoreGame.ToolTip.4")));
            }
            if (item.type == ItemID.PinaColada)
            {
                tooltips.Add(new TooltipLine(Mod, "21", Language.GetTextValue("Mods.HardcoreGame.ToolTip.4")));
            }
            if (item.type == ItemID.PrismaticPunch)
            {
                tooltips.Add(new TooltipLine(Mod, "22", Language.GetTextValue("Mods.HardcoreGame.ToolTip.4")));
            }
            if (item.type == ItemID.RoastedBird)
            {
                tooltips.Add(new TooltipLine(Mod, "23", Language.GetTextValue("Mods.HardcoreGame.ToolTip.3")));
            }
            if (item.type == ItemID.RoastedDuck)
            {
                tooltips.Add(new TooltipLine(Mod, "24", Language.GetTextValue("Mods.HardcoreGame.ToolTip.5")));
            }
            if (item.type == ItemID.SauteedFrogLegs)
            {
                tooltips.Add(new TooltipLine(Mod, "25", Language.GetTextValue("Mods.HardcoreGame.ToolTip.3")));
            }
            if (item.type == ItemID.SeafoodDinner)
            {
                tooltips.Add(new TooltipLine(Mod, "26", Language.GetTextValue("Mods.HardcoreGame.ToolTip.6")));
            }
            if (item.type == ItemID.SmoothieofDarkness)
            {
                tooltips.Add(new TooltipLine(Mod, "27", Language.GetTextValue("Mods.HardcoreGame.ToolTip.4")));
            }
            if (item.type == ItemID.TropicalSmoothie)
            {
                tooltips.Add(new TooltipLine(Mod, "28", Language.GetTextValue("Mods.HardcoreGame.ToolTip.4")));
            }
            if (item.type == ItemID.PumpkinPie)
            {
                tooltips.Add(new TooltipLine(Mod, "29", Language.GetTextValue("Mods.HardcoreGame.ToolTip.3")));
            }
            if (item.type == ItemID.Teacup)
            {
                tooltips.Add(new TooltipLine(Mod, "30", Language.GetTextValue("Mods.HardcoreGame.ToolTip.4")));
            }
            if (item.type == ItemID.Sashimi)
            {
                tooltips.Add(new TooltipLine(Mod, "31", Language.GetTextValue("Mods.HardcoreGame.ToolTip.3")));
            }
            if (item.type == 5275)
            {
                tooltips.Add(new TooltipLine(Mod, "32", Language.GetTextValue("Mods.HardcoreGame.ToolTip.4")));
            }
            if (item.type == ItemID.Pho)
            {
                tooltips.Add(new TooltipLine(Mod, "33", Language.GetTextValue("Mods.HardcoreGame.ToolTip.5")));
            }
            if (item.type == ItemID.PadThai)
            {
                tooltips.Add(new TooltipLine(Mod, "34", Language.GetTextValue("Mods.HardcoreGame.ToolTip.5")));
            }
            if (item.type == ItemID.Marshmallow)
            {
                tooltips.Add(new TooltipLine(Mod, "35", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
            }
            if (item.type == ItemID.SugarCookie)
            {
                tooltips.Add(new TooltipLine(Mod, "36", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
            }
            if (item.type == ItemID.GingerbreadCookie)
            {
                tooltips.Add(new TooltipLine(Mod, "37", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
            }
            if (item.type == ItemID.ChristmasPudding)
            {
                tooltips.Add(new TooltipLine(Mod, "38", Language.GetTextValue("Mods.HardcoreGame.ToolTip.3")));
            }
            if (item.type == ItemID.Steak)
            {
                tooltips.Add(new TooltipLine(Mod, "39", Language.GetTextValue("Mods.HardcoreGame.ToolTip.5")));
            }
            if (item.type == ItemID.Spaghetti)
            {
                tooltips.Add(new TooltipLine(Mod, "40", Language.GetTextValue("Mods.HardcoreGame.ToolTip.3")));
            }
            if (item.type == ItemID.ShuckedOyster)
            {
                tooltips.Add(new TooltipLine(Mod, "41", Language.GetTextValue("Mods.HardcoreGame.ToolTip.3")));
            }
            if (item.type == ItemID.ShrimpPoBoy)
            {
                tooltips.Add(new TooltipLine(Mod, "42", Language.GetTextValue("Mods.HardcoreGame.ToolTip.5")));
            }
            if (item.type == ItemID.PotatoChips)
            {
                tooltips.Add(new TooltipLine(Mod, "43", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
            }
            if (item.type == ItemID.Pizza)
            {
                tooltips.Add(new TooltipLine(Mod, "44", Language.GetTextValue("Mods.HardcoreGame.ToolTip.5")));
            }
            if (item.type == ItemID.Nachos)
            {
                tooltips.Add(new TooltipLine(Mod, "45", Language.GetTextValue("Mods.HardcoreGame.ToolTip.5")));
            }
            if (item.type == ItemID.Milkshake)
            {
                tooltips.Add(new TooltipLine(Mod, "46", Language.GetTextValue("Mods.HardcoreGame.ToolTip.4")));
            }
            if (item.type == ItemID.IceCream)
            {
                tooltips.Add(new TooltipLine(Mod, "47", Language.GetTextValue("Mods.HardcoreGame.ToolTip.4")));
            }
            if (item.type == ItemID.Hotdog)
            {
                tooltips.Add(new TooltipLine(Mod, "48", Language.GetTextValue("Mods.HardcoreGame.ToolTip.3")));
            }
            if (item.type == ItemID.Grapes)
            {
                tooltips.Add(new TooltipLine(Mod, "49", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
                tooltips.Add(new TooltipLine(Mod, "50", Language.GetTextValue("Mods.HardcoreGame.ToolTip.2")));
            }
            if (item.type == ItemID.Fries)
            {
                tooltips.Add(new TooltipLine(Mod, "51", Language.GetTextValue("Mods.HardcoreGame.ToolTip.3")));
            }
            if (item.type == ItemID.FriedEgg)
            {
                tooltips.Add(new TooltipLine(Mod, "52", Language.GetTextValue("Mods.HardcoreGame.ToolTip.3")));
            }
            if (item.type == ItemID.CreamSoda)
            {
                tooltips.Add(new TooltipLine(Mod, "53", Language.GetTextValue("Mods.HardcoreGame.ToolTip.4")));
            }
            if (item.type == ItemID.CoffeeCup)
            {
                tooltips.Add(new TooltipLine(Mod, "54", Language.GetTextValue("Mods.HardcoreGame.ToolTip.4")));
            }
            if (item.type == ItemID.ChocolateChipCookie)
            {
                tooltips.Add(new TooltipLine(Mod, "55", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
            }
            if (item.type == ItemID.ChickenNugget)
            {
                tooltips.Add(new TooltipLine(Mod, "56", Language.GetTextValue("Mods.HardcoreGame.ToolTip.3")));
            }
            if (item.type == ItemID.MilkCarton)
            {
                tooltips.Add(new TooltipLine(Mod, "57", Language.GetTextValue("Mods.HardcoreGame.ToolTip.4")));
            }
            if (item.type == ItemID.Burger)
            {
                tooltips.Add(new TooltipLine(Mod, "58", Language.GetTextValue("Mods.HardcoreGame.ToolTip.5")));
            }
            if (item.type == ItemID.BBQRibs)
            {
                tooltips.Add(new TooltipLine(Mod, "59", Language.GetTextValue("Mods.HardcoreGame.ToolTip.5")));
            }
            if (item.type == ItemID.BananaSplit)
            {
                tooltips.Add(new TooltipLine(Mod, "60", Language.GetTextValue("Mods.HardcoreGame.ToolTip.4")));
            }
            if (item.type == ItemID.Bacon)
            {
                tooltips.Add(new TooltipLine(Mod, "61", Language.GetTextValue("Mods.HardcoreGame.ToolTip.3")));
            }
            if (item.type == ItemID.ApplePie)
            {
                tooltips.Add(new TooltipLine(Mod, "62", Language.GetTextValue("Mods.HardcoreGame.ToolTip.3")));
            }
            if (item.type == 5277)
            {
                tooltips.Add(new TooltipLine(Mod, "63", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
            }
            if (item.type == ItemID.Starfruit)
            {
                tooltips.Add(new TooltipLine(Mod, "64", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
                tooltips.Add(new TooltipLine(Mod, "65", Language.GetTextValue("Mods.HardcoreGame.ToolTip.2")));
            }
            if (item.type == ItemID.Rambutan)
            {
                tooltips.Add(new TooltipLine(Mod, "65", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
                tooltips.Add(new TooltipLine(Mod, "66", Language.GetTextValue("Mods.HardcoreGame.ToolTip.2")));
            }
            if (item.type == 5278)
            {
                tooltips.Add(new TooltipLine(Mod, "66", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
                tooltips.Add(new TooltipLine(Mod, "67", Language.GetTextValue("Mods.HardcoreGame.ToolTip.2")));
            }
            if (item.type == ItemID.Plum)
            {
                tooltips.Add(new TooltipLine(Mod, "67", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
                tooltips.Add(new TooltipLine(Mod, "68", Language.GetTextValue("Mods.HardcoreGame.ToolTip.2")));
            }
            if (item.type == ItemID.Pineapple)
            {
                tooltips.Add(new TooltipLine(Mod, "68", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
                tooltips.Add(new TooltipLine(Mod, "69", Language.GetTextValue("Mods.HardcoreGame.ToolTip.2")));
            }
            if (item.type == ItemID.Peach)
            {
                tooltips.Add(new TooltipLine(Mod, "69", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
                tooltips.Add(new TooltipLine(Mod, "70", Language.GetTextValue("Mods.HardcoreGame.ToolTip.2")));
            }
            if (item.type == ItemID.Mango)
            {
                tooltips.Add(new TooltipLine(Mod, "70", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
                tooltips.Add(new TooltipLine(Mod, "71", Language.GetTextValue("Mods.HardcoreGame.ToolTip.2")));
            }
            if (item.type == ItemID.Lemon)
            {
                tooltips.Add(new TooltipLine(Mod, "71", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
                tooltips.Add(new TooltipLine(Mod, "72", Language.GetTextValue("Mods.HardcoreGame.ToolTip.2")));
            }
            if (item.type == ItemID.Grapefruit)
            {
                tooltips.Add(new TooltipLine(Mod, "72", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
                tooltips.Add(new TooltipLine(Mod, "73", Language.GetTextValue("Mods.HardcoreGame.ToolTip.2")));
            }
            if (item.type == ItemID.Elderberry)
            {
                tooltips.Add(new TooltipLine(Mod, "73", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
                tooltips.Add(new TooltipLine(Mod, "74", Language.GetTextValue("Mods.HardcoreGame.ToolTip.2")));
            }
            if (item.type == ItemID.Dragonfruit)
            {
                tooltips.Add(new TooltipLine(Mod, "74", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
                tooltips.Add(new TooltipLine(Mod, "75", Language.GetTextValue("Mods.HardcoreGame.ToolTip.2")));
            }
            if (item.type == ItemID.Coconut)
            {
                tooltips.Add(new TooltipLine(Mod, "75", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
                tooltips.Add(new TooltipLine(Mod, "76", Language.GetTextValue("Mods.HardcoreGame.ToolTip.2")));
            }
            if (item.type == ItemID.Cherry)
            {
                tooltips.Add(new TooltipLine(Mod, "76", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
                tooltips.Add(new TooltipLine(Mod, "77", Language.GetTextValue("Mods.HardcoreGame.ToolTip.2")));
            }
            if (item.type == ItemID.BloodOrange)
            {
                tooltips.Add(new TooltipLine(Mod, "77", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
                tooltips.Add(new TooltipLine(Mod, "78", Language.GetTextValue("Mods.HardcoreGame.ToolTip.2")));
            }
            if (item.type == ItemID.BlackCurrant)
            {
                tooltips.Add(new TooltipLine(Mod, "78", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
                tooltips.Add(new TooltipLine(Mod, "79", Language.GetTextValue("Mods.HardcoreGame.ToolTip.2")));
            }
            if (item.type == ItemID.Banana)
            {
                tooltips.Add(new TooltipLine(Mod, "79", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
                tooltips.Add(new TooltipLine(Mod, "80", Language.GetTextValue("Mods.HardcoreGame.ToolTip.2")));
            }
            if (item.type == ItemID.Apricot)
            {
                tooltips.Add(new TooltipLine(Mod, "80", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
                tooltips.Add(new TooltipLine(Mod, "81", Language.GetTextValue("Mods.HardcoreGame.ToolTip.2")));
            }
            if (item.type == ItemID.Apple)
            {
                tooltips.Add(new TooltipLine(Mod, "81", Language.GetTextValue("Mods.HardcoreGame.ToolTip.1")));
                tooltips.Add(new TooltipLine(Mod, "82", Language.GetTextValue("Mods.HardcoreGame.ToolTip.2")));
            }
        }
        private static string GetFreshnessStars(float freshness)
        {
            int fullStars = freshness switch
            {
                > 80f => 5,
                > 60f => 4,
                > 40f => 3,
                > 20f => 2,
                > 0f => 1,
                _ => 0
            };
            string stars = "";
            for (int i = 0; i < 5; i++)
            {
                int starItemId = i < fullStars ? ModContent.ItemType<Icon_Rank_Light>() : ModContent.ItemType<Icon_Rank_Dim>();
                stars += $"[i:{starItemId}]";
            }
            return $"{stars} ({freshness:F0}%)";
        }
    }
}
